﻿namespace CSharpPro._1103;

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

class UserService : IUserService
{
    private User currentUser; 

    public void Register(List<User> userList)
    {
        Console.WriteLine("===== 用户注册 =====");
        while (true)
        {
            Console.WriteLine("请输入用户名（3 - 15个字符，只能包含字母、数字、下划线）：");
            string inputUsername = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(inputUsername))
            {
                Console.WriteLine("用户名不可为空！");
                continue;
            }
            
            foreach (var user in userList)
            {
                if (user.Username == inputUsername)
                {
                    Console.WriteLine("用户名已存在，请重新输入！");
                    goto InputUsername; 
                }
            }

            if (!Regex.IsMatch(inputUsername, @"^[a-zA-Z0-9_]{3,15}$"))
            {
                Console.WriteLine("用户名格式不符合要求，请重新输入！");
                continue;
            }

            Console.WriteLine("请输入密码（6 - 20个字符，必须包含至少1个大写字母、1个小写字母和1个数字）：");
            string inputPassword = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(inputPassword))
            {
                Console.WriteLine("密码不可为空！");
                continue;
            }

            if (!Regex.IsMatch(inputPassword, @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)[A-Za-z\d]{6,20}$"))
            {
                Console.WriteLine("密码格式不符合要求，请重新输入！");
                continue;
            }

            Console.WriteLine("请确认密码：");
            string confirmPassword = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(confirmPassword))
            {
                Console.WriteLine("确认密码不可为空！");
                continue;
            }

            if (inputPassword != confirmPassword)
            {
                Console.WriteLine("密码与确认密码不一致，请重新输入！");
                continue;
            }
            
            userList.Add(new User(inputUsername, inputPassword));
            Console.WriteLine("注册成功！");
            break;

        InputUsername:; 
        }
    }

    public bool Login(List<User> userList)
    {
        Console.WriteLine("===== 用户登录 =====");
        Console.WriteLine("请输入用户名：");
        string inputUsername = Console.ReadLine();

        if (string.IsNullOrWhiteSpace(inputUsername))
        {
            Console.WriteLine("用户名不可为空！");
            return false;
        }

        Console.WriteLine("请输入密码：");
        string inputPassword = Console.ReadLine();

        if (string.IsNullOrWhiteSpace(inputPassword))
        {
            Console.WriteLine("密码不可为空！");
            return false;
        }
        
        foreach (var user in userList)
        {
            if (user.Username == inputUsername && user.Password == inputPassword)
            {
                currentUser = user;
                Console.WriteLine("登录成功！进入系统首页（模拟）");
                return true;
            }
        }

        Console.WriteLine("用户名或密码错误！");
        return false;
    }
    
    public void ShowMainMenu(List<User> userList)
    {
        while (true)
        {
            Console.WriteLine("\n===== 主菜单 =====");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.WriteLine("请输入选项（1 - 3）：");

            string choiceStr = Console.ReadLine();
            int choice;

            if (!int.TryParse(choiceStr, out choice))
            {
                Console.WriteLine("请输入有效数字！");
                continue;
            }

            switch (choice)
            {
                case 1:
                    Login(userList);
                    break;
                case 2:
                    Register(userList);
                    break;
                case 3:
                    Console.WriteLine("退出程序！");
                    return;
                default:
                    Console.WriteLine("输入错误，请重新输入！");
                    break;
            }
        }
    }
}